package com.damage.admin.web;

import com.damage.admin.service.IEmployeeService;
import com.damage.common.entity.Employee;
import com.damage.common.exception.UniqueException;
import com.damage.common.object.BaseResp;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@ResponseBody
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @PostMapping("/new")
    public BaseResp newEmployee(Employee employee){
        try {
            Integer  result = employeeService.newEmployee(employee);
            if (result > 0){
                return BaseResp.ok();
            }else {
                return BaseResp.error();
            }
        } catch (UniqueException e) {
            return BaseResp.error(e.getMessage());
        }
    }

    @GetMapping("/findAll")
    public BaseResp findAll(Integer pageNum){
        PageInfo<Employee> result = employeeService.findAll(pageNum);
        if (result != null){
            return BaseResp.ok(result);
        }else{
            return BaseResp.error();
        }
    }

    @GetMapping("/findEmployee")
    public BaseResp findEmployee(Integer department,String name,Integer pageNum){
        PageInfo<Employee> result = employeeService.findEmployee(pageNum, department, name);
        if (result != null){
            return BaseResp.ok(result);
        }else{
            return BaseResp.error();
        }
    }

    @GetMapping("/findById")
    public BaseResp findById(Integer id){
        Employee employee = employeeService.findById(id);
        if (employee != null){
            return BaseResp.ok(employee);
        }else{
            return BaseResp.error();
        }
    }

    @PostMapping("/update")
    public BaseResp updateEmployee(Employee employee){
        Integer result = employeeService.update(employee);
        if (result > 0){
            return BaseResp.ok();
        }else{
            return BaseResp.error();
        }
    }

    @PostMapping("/delete")
    public BaseResp deleteEmployee(Integer id){
        Integer result = employeeService.deleteById(id);
        if (result > 0){
            return BaseResp.ok();
        }else{
            return BaseResp.error();
        }
    }
}
